package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Model;

import java.util.List;

/**
 * Created by quxingyu on 2015/8/6.
 */
public class TestpaperItemResult extends Model<TestpaperItemResult> {
    public static final TestpaperItemResult dao = new TestpaperItemResult();
    /**
     * 查看学员已经答了哪些题目
     * @param testpaperId 试卷ID
     * @param studentId 学员ID
     * */
    public List getQuestionList(Long testpaperId,Long studentId){
        return dao.find("select c.* from t_testpaper_item_result c where c.testpaper_id = ? and c.student_id = ?",testpaperId,studentId);
    }

    /**
     * 保存选项结果表
     * @param itemResult
     * @param testpaper_item_id
     * @param testpaper_id
     * @param testpaper_result_id
     * @param student_id
     * @param question_id
     * @param status
     * @param score
     * @param answer
     */
    public void saveTestpaperItemResult(TestpaperItemResult itemResult,Long testpaper_item_id,Long testpaper_id,Long testpaper_result_id,
                                        Long student_id,Long question_id,int status,int score,String answer){
        itemResult.set("testpaper_item_id",testpaper_item_id);
        itemResult.set("testpaper_id",testpaper_id);
        itemResult.set("testpaper_result_id",testpaper_result_id);
        itemResult.set("student_id",student_id);
        itemResult.set("question_id",question_id);
        itemResult.set("status",status);
        itemResult.set("score",score);
        itemResult.set("answer",answer);
        itemResult.remove("id").save();
    }

    /**
     * 判断是否已存在答题
     * @param testperId
     * @param testPaperItemId
     * @param studentId
     * @param questionId
     * @return
     */
    public TestpaperItemResult findByAnsweredQuestionId(Long testperId,Long testPaperItemId,Long studentId,Long questionId){
        return dao.findFirst("select t.* from t_testpaper_item_result t " +
                "where t.testpaper_item_id =? and t.testpaper_id = ? and student_id = ? and question_id = ?",
                testPaperItemId,testperId,studentId,questionId);
    }

    /**
     * 更新选项
     */
    public void updateItemResult(TestpaperItemResult testpaperItemResult,int status,int getScore,String myAnswer){
        testpaperItemResult.set("status",status);
        testpaperItemResult.set("score",getScore);
        testpaperItemResult.set("answer",myAnswer);
        testpaperItemResult.update();
    }
}
